<script src="{{ getAssets }}js/bivariatetable/bivariatetable.js" type="text/javascript"></script>
<script type="text/javascript">
var goods_id = '{{.goods_id}}';
var entry_id = '{{.entry_id}}';
var middle_id = '{{.middle_id}}';
var goods_function_info;
var goods_mouting_genre;
var typeid = 30;
$(function(){
	$.post('{{ urlfor "ErpEntryController.DrugsAdd" }}',{'goods_id':goods_id, 'middle_id': middle_id}, function(res){
		if(res.code == 0) {
			$('#goods-title').html(productGroupName(res.data.goods));
			middle_data = res.data.middle;
			typeid = res.data.goods.TypeId;
            if(middle_data == null) {
            } else {
                $('#attrlist').html('');
                $('#addsndselect').hide();
                $.each(middle_data, function(index, value){
                    addTrLine(value);
                });
            }
            colShowHide();
		}
	}, 'json');
});

function colShowHide() {
	if(typeid == 30) {
		$('.f40').hide();
	} else if(typeid == 41){
		$('.f40').show();
	}	
}
function setOptions(data ,def) {
	var opt = '<option value=""></option>';
	$.each(data, function(index, value){
		opt += '<option value="'+ value.val +'" '+ (def==value.val ? 'selected':'') +'>'+ value.val +'</option>';
	});
	return opt;
}

function addTrLine(data) {
    console.log(data);
    var tr = '<tr><td><input type="checkbox" value=""  class="checkboxs"/><input type="hidden" value="'+ (data.Snd ? data.Snd.id : 0) +'" class="snd_id"/></td>\
             <td><input name="norms[]" class="norms" type="text" value="'+ (data.Snd ? data.Snd.norms :'') +'" /></td>\
             <td><input name="place_origin[]" class="place_origin" type="text" value="'+ (data.Snd ? data.Snd.place_origin :'') +'" /></td>\
             <td><input name="dnumber[]" class="dnumber" type="text"  value="'+ (data.Snd ? data.Snd.dnumber :'') +'" /></td>\
			 <td><input name="purchase_num[]" class="purchase_num" type="number"  value="'+ data.purchase_num +'" /></td></tr>';
	$('#attrlist').append(tr);		 
	return tr;
}

function setSelectSndLine(data) {
    $('#attrlist').html('');
    $.each(data, function(index, value){
        var isset = $('.snd_id').find('input[value="'+ value.id +'"]');
        if(isset.length <= 0) {
            var _data = {};
            _data.Snd = value;
            _data.purchase_num = 1;
            addTrLine(_data);
        }
    });
    colShowHide();
}

$('#addline').click(function(){
	addTrLine({});
	colShowHide();
	return false;	
});

$('#delline').click(function(){
	var checkboxs = $('.checkboxs:checked');
	if(checkboxs.length > 0) {
		checkboxs.each(function(index, element) {
           $(this).parent().parent().remove(); 
        });
	}
	return false;	
});

$('#addsndselect').click(function () {
    var url = '{{urlfor "ProductsController.SndList"}}?product_id='+goods_id;
    layer.open({
        type: 2,
        shade: 0.2,
        title:false,
        area: ['80%', '90%'],
        maxmin: false,
        content: url,
        zIndex: parent.layer.zIndex,
        success: function(layero){
            parent.layer.setTop(layero);
        }
    });
});

$('#submitBtn').click(function(){
	var tr = $('#attrlist > tr');
	var yesLine = 0;
	var yesSubmit = true;
	var totoalnumber = 0;
	var dataInfo = new Array();
	tr.each(function(index,value){
		var valueObj = $(value);
		var norms = valueObj.find('.norms').val();
		var place_origin = valueObj.find('.place_origin').val();
		var dnumber = valueObj.find('.dnumber').val();
		var purchase_num = valueObj.find('.purchase_num').val();
		var snd_id = valueObj.find('.snd_id').val();
		if(purchase_num != '') {
			totoalnumber += Number(purchase_num);
			dataInfo.push({'SndId':Number(snd_id),'Norms':norms,'place_origin':place_origin,'dnumber':dnumber ,'purchase_num':Number(purchase_num)});
			yesLine++;
		}
	});
	if(!yesSubmit) {
	    return false;
    }
	if(yesSubmit && yesLine==0){
		layer.alert('请填入数据再保存');
		return;
	}
	$.post('{{ urlfor "ErpEntryController.SaveEntryData" }}', {'purchase_num': totoalnumber, 'dataInfo':JSON.stringify(dataInfo), 'middle_id': middle_id,'goods_id':goods_id,'entry_id':entry_id}, function(res){
		if(res.code == 1) {
			layer.alert(res.msg);
			return false;	
		}
		parent.closeSelectComplete();
	}, 'json');
	
	return false;
});
</script>